home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cc02.zip / FXU.C < prev   
Text File  |  1985-07-01  |  5KB  |  172 lines

  1. /**
  2. * name         fxu -- function extract utility
  3. *
  4. * usage        fxu filename function
  5. *
  6. *              where "filename" is the name of a file containing
  7. *              several C functions, and "function" is the name of
  8. *              the particular function to be extracted.  If the
  9. *              named function is found, then (1) standard input is
  10. *              copied to the standard output until EOF, and (2) the
  11. *              text of the named function is written to the standard
  12. *              output.  The first option allows header information
  13. *              to be prepended to the output file.
  14. *
  15. **/
  16.  
  17. #include "stdio.h"
  18. #include "ctype.h"
  19.  
  20. #define MAX 16        /* maximum characters in function name */
  21. #define MAXBUF 2000   /* maximum characters buffered between functions */
  22.  
  23. unsigned _stack = MAXBUF + 2000;    /* reserve sufficient stack */
  24.  
  25. main(argc, argv)
  26. int argc;
  27. char *argv[];
  28. {
  29. int c, brace, cnest, nc;
  30. int i, ns, copy, inlit, delim, pc;
  31. FILE *sfp;
  32. char symbol[MAX+1];
  33. char text[MAXBUF];
  34.  
  35. if (argc != 3)
  36.    {
  37.    fputs("Usage: fxu filename function\n", stderr);
  38.    exit(1);
  39.    }
  40. if ((sfp = fopen(argv[1], "r")) == NULL)
  41.    {
  42.    fputs("Can't open source file\n", stderr);
  43.    exit(1);
  44.    }
  45. brace = cnest = nc = ns = copy = inlit = pc = 0;
  46. c = getc(sfp);        /* get first char */
  47. while (c != EOF)
  48.    {                 /* scan through source file */
  49.    if (ns == MAXBUF)
  50.        {
  51.        fputs("Maximum buffer size exceeded\n", stderr);
  52.        exit(1);
  53.        }
  54.    if (copy == 0)
  55.        {
  56.        if (brace == 0) text[ns++] = c;  /* save chars between functions */
  57.        }
  58.    else
  59.        if (putchar(c) == EOF)
  60.            {
  61.            fputs("Copy error\n", stderr);
  62.            exit(1);
  63.            }
  64.    if (c == '/')
  65.        {             /* possible comment */
  66.        nc = 0;
  67.        if ((c = getc(sfp)) == '*')
  68.            {
  69.            ++cnest;   /* bump nesting level */
  70.            if (copy) putchar(c);
  71.            else if (brace == 0) text[ns++] = c;
  72.            c = getc(sfp);
  73.            }
  74.        continue;
  75.        }
  76.    if (cnest != 0)
  77.        {             /* inside comment */
  78.        if (c == '*')
  79.            {
  80.            if ((c = getc(sfp)) == '/')
  81.               {
  82.               --cnest;       /* reduce nesting level */
  83.               if (copy) putchar(c);
  84.               else if (brace == 0) text[ns++] = c;
  85.               c = getc(sfp);
  86.               }
  87.            continue;
  88.            }
  89.        nc = 0;
  90.        }
  91.    else if (inlit)
  92.        {               /* inside literal string */
  93.        if (c == '\\' && pc == '\\') c = 0;
  94.        if (c == delim && pc != '\\') inlit = 0;
  95.        pc = c;         /* save previous character */
  96.        }
  97.    else if (c == '\'' || c == '\"')
  98.        {               /* enter literal string */
  99.        inlit = 1;
  100.        pc = 0;
  101.        delim = c;
  102.        }
  103.    else if (c == '{') ++brace;
  104.    else if (c == '}')
  105.        {             /* right brace */
  106.        nc = 0;
  107.        if (--brace == 0)
  108.            if (copy == 0) ns = 0;      /* reset save index if not found */
  109.            else
  110.                {               /* copy complete */
  111.                putchar('\n');
  112.                exit(0);
  113.                }
  114.        }
  115.    else if (brace == 0)
  116.        {
  117.        if (nc == 0)
  118.            {             /* symbol not started yet */
  119.            if (iscsymf(c))
  120.                symbol[nc++] = c;  /* start new symbol */
  121.            }
  122.        else if (iscsym(c) || c == '$')
  123.                      /* continue symbol */
  124.            if (nc < MAX) symbol[nc++] = c;
  125.            else symbol[0] = '\0';
  126.        else if (nc != 0)
  127.            {             /* end of current symbol */
  128.            symbol[nc++] = '\0';
  129.            if (symcmp(symbol,argv[2]) == 0)
  130.                {               /* named function has been found */
  131.                while ((c = getchar()) != EOF)
  132.                    putchar(c);         /* copy standard input to output */
  133.                for (i = 0; i < ns; i++)
  134.                    putchar(text[i]);           /* copy saved characters */
  135.                copy = 1;       /* turn on copy flag */
  136.                }
  137.            nc = 0;
  138.            }
  139.        }
  140.    c = getc(sfp);      /* get next char */
  141.    }
  142.  
  143. fputs("Named function not found\n", stderr);
  144. exit(1);
  145. }
  146. /**
  147. *
  148. * name         symcmp -- compare symbols
  149. *
  150. * synopsis     retval = symcmp(p, q);
  151. *              int retval;            return code: 0 if equal, 1 if not
  152. *              char *p, *q;           symbols to be compared
  153. *
  154. * description  This function compares two alphanumeric symbols for
  155. *              identity.  Upper and lower case alphabetic characters
  156. *              are considered equivalent by this function.
  157. *
  158. **/
  159. symcmp(p,q)
  160. char *p, *q;
  161. {
  162. int a, b;
  163.  
  164. while ((toupper(*p)) == (toupper(*q)))
  165. {
  166. if (*p == '\0') return(0);
  167. p++;
  168. q++;
  169. }
  170. return(1);
  171. }
  172.